package com.vanysoft.business.base.team.controller;

import com.vanysoft.business.base.team.entity.TBaseTeam;
import com.vanysoft.business.base.team.service.BaseTeamService;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.common.utils.string.StringUtil;
import com.vanysoft.framework.aop.VanySoftAspectException;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 类名称：BaseTeamAction
 * 类描述：群组类
 * 创建人：SharpClaw_x
 * 创建时间：2017年5月26日 上午11:03:02
 * 修改人：SharpClaw_x
 * 修改时间：2017年5月26日 上午11:03:02
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 */
@RestController
@RequestMapping("/base/team")
public class BaseTeamAction extends BaseController {

    @Autowired
    private BaseTeamService teamService;

    /**
     * Function Name               addTeam
     *
     * @param team
     * @return
     * @throws VanySoftBaseException
     * @description 添加群组
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月26日 上午11:26:17           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JSONResponse addTeam(TBaseTeam team, @SessionUser UserInfo userInfo) throws VanySoftBaseException {
        TBaseTeam testeam = teamService.addTeam(team, userInfo);
        return new JSONResponse(testeam);
    }

    /**
     * Function Name               modifyTeam
     *
     * @param request
     * @param team
     * @return
     * @throws VanySoftAspectException
     * @description 修改群组
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月26日 下午2:17:53           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public JSONResponse modifyTeam(HttpServletRequest request, TBaseTeam team) throws VanySoftAspectException {
        String rspCode = teamService.modifyTeam(team);
        return new JSONResponse(rspCode);
    }

    /**
     * Function Name               deleteTeam
     *
     * @param request
     * @param teamCode
     * @return
     * @throws VanySoftAspectException
     * @description 删除群组
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月26日 下午3:37:41           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JSONResponse deleteTeam(HttpServletRequest request, String teamCode) throws VanySoftAspectException {
        teamService.deleteTeam(teamCode);
        return new JSONResponse();
    }

    /**
     * Function Name               queryTeamUsers
     *
     * @param request
     * @param teamCode
     * @param pageIndex
     * @param pageSize
     * @return
     * @description 查询群组下的人
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月26日 下午6:34:08           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/queryTeamer", method = RequestMethod.GET)
    public JSONResponse queryTeamUsers(HttpServletRequest request, String teamCode, String searchValue, String pageIndex, String pageSize) {
        Integer _pageIndex = StringUtil.isEmpty(pageIndex) ? 1 : Integer.valueOf(pageIndex);
        Integer _pageSize = StringUtil.isEmpty(pageSize) ? 20 : Integer.valueOf(pageSize);

        Map<String, Object> list = teamService.queryTeamUsers(teamCode, searchValue, _pageIndex, _pageSize);

        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, list);
    }

    /**
     * Function Name               loadTeams
     *
     * @return
     * @throws VanySoftBaseException
     * @description 加载我的群组列表
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月27日 上午9:53:47           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/load", method = RequestMethod.GET)
    public JSONResponse loadTeams(@SessionUser UserInfo userInfo) throws VanySoftBaseException {
        String userId = userInfo.getId();
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, teamService.loadMyTeams(userId));// FIXME **
    }

    /**
     * Function Name               addTeamUsers
     *
     * @param teamCode
     * @return
     * @description 选择人员添加到群组
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月27日 上午9:54:08           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/addTeamUsers")
    public JSONResponse addTeamUsers(String userIds, String teamCode) {
        teamService.addTeamUsers(userIds, teamCode);
        return new JSONResponse();
    }

    /**
     * Function Name               deleteTeamUsersBatch
     *
     * @description 批量删除群组人员
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月19日 上午9:58:03           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/delTeamUsesr", method = RequestMethod.POST)
    public JSONResponse deleteTeamUsersBatch(String teamUserIds) {
        teamService.delTeamUserBatch(teamUserIds);
        return new JSONResponse();
    }

    /**
     * Function Name               modifyTeamStatus
     *
     * @param teamCode
     * @param isEnable
     * @return
     * @description 启用、禁用群组
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月1日 下午5:23:30           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/modifyTeamStatus", method = RequestMethod.POST)
    public JSONResponse modifyTeamStatus(String teamCode, String isEnable) {
        String rspCode = teamService.modifyTeamStatus(teamCode, isEnable);// FIXME **
        return new JSONResponse(rspCode);
    }

    /**
     * Function Name               loadCanSeeTeamsInfoByUserCode
     *
     * @return
     * @throws VanySoftBaseException
     * @description 根据用户代码查询可以看到的群组信息集合
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月6日 下午9:21:18          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/loadCanSeeTeamsInfoByUserCode")
    public JSONResponse loadCanSeeTeamsInfoByUserCode(@SessionUser UserInfo userInfo) throws VanySoftBaseException {
        List<Map<String, Object>> listMap = teamService.loadCanSeeTeamsInfoByUserCode(userInfo.getId());
        return new JSONResponse(listMap);
    }

    /**
     * Function Name               loadFunctionByTeamCode
     *
     * @return
     * @throws VanySoftBaseException
     * @description 根据群组代码及当前登录人员代码查询已分配功能权限和未分配功能权限
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月7日 上午10:44:07          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/loadFunctionByTeamCode")
    public JSONResponse loadFunctionByTeamCode(@SessionUser UserInfo userInfo, String teamCode) throws VanySoftBaseException {
        Map<String, Object> map = teamService.loadFunctionByTeamCode(userInfo.getId(), teamCode);
        return new JSONResponse(map);
    }


    /**
     * Function Name               modifyTeamFunctionAllotAndRemove
     *
     * @param request
     * @return
     * @description 群组功能分配与移除
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月7日 上午10:44:39          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/modifyTeamFunctionAllotAndRemove")
    public JSONResponse modifyTeamFunctionAllotAndRemove(HttpServletRequest request) {
        String teamCode = request.getParameter("teamCode");
        String functionCodes = request.getParameter("functionCodes");
        teamService.modifyTeamFunctionAllotAndRemove(teamCode, functionCodes);
        return new JSONResponse();
    }


    /**
     * Function Name               loadAllTeams
     *
     * @return
     * @throws VanySoftBaseException
     * @description (消息发送加载所有群组 - 消息中心)
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年8月29日 下午12:03:01          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/loadAllTeams", method = RequestMethod.GET)
    public JSONResponse loadAllTeams(@SessionUser UserInfo userInfo) throws VanySoftBaseException {
        List<Map<String, Object>> teamList = teamService.queryAllTeam();
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, teamList);
    }

}
